package com.example.lastwork;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FristFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FristFragment extends Fragment{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    List<Map<String,String>> data_list = new ArrayList<>();
    private String TAG = "first";
    private View rootView;
    public Context context;
    private String mParam1;
    private String mParam2;
    private static FristFragment mf;
    private String text = "";


    public static FristFragment getFristFragment()
    {
        if(mf == null){
            mf = new FristFragment();
        }
        return mf;
    }

    public FristFragment() {
        // Required empty public constructor
    }
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SecondFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FristFragment newInstance(String param1, String param2) {
        FristFragment fragment = new FristFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_frist, container, false);
            initView(rootView);// 控件初始化
        }
        Log.i(TAG,"连接成功");
        GridView gridView = rootView.findViewById(R.id.main);
        new Thread(){
            @Override
            public void run() {
                try {
                    UserDao userDao = new UserDao();
                    userDao.getData(data_list);
                    Log.i(TAG,"成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "失败");
                }
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        String [] from ={"name","title","id"};
                        int [] to = {R.id.T_id,R.id.title,R.id.T_myid};
                        SimpleAdapter adapter = new SimpleAdapter(getContext(), data_list, R.layout.gridviewcontent, from, to);
                        gridView.setAdapter(adapter);
                        Log.i(TAG, "线程运行");
                    }
                });
            }

        }.start();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // 跳转到另一个界面查看商品详细信息
                Intent intent = new Intent(getActivity(),
                        details.class);
                String num = data_list.get(position).get("num");
                Log.i(TAG,"num====="+num);
                intent.putExtra("num",num);
                String id1 =data_list.get(position).get("id");
                intent.putExtra("id",id1);
                startActivity(intent);
            }
        });
        return  rootView;
    }

    private void initView(View rootView) {
        rootView = null;
        return;
    }

    public void onPause() {
        super.onPause();
    }
}